# Copyright 2020 huanggefan.cn
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

.PYONY: all, test, clean, sync

SHELL=/bin/bash
PREFIX = dist

BUILD_TAG=$(shell git describe --always)
BUILD_TIME=$(shell date +%F@%T)
BUILD_GO=$(shell go version)

LDFLAGS = -ldflags '-X "main.BuildTag=${BUILD_TAG}" -X "main.BuildGo=${BUILD_GO}" -X "main.BuildTime=${BUILD_TIME}"'
GLOBAL=./cmd/global.go

all: clean
	@mkdir -p ${PREFIX}
	go build ${LDFLAGS} -o ${PREFIX}/init_database ${GLOBAL} ./cmd/init_database.go
	go build ${LDFLAGS} -o ${PREFIX}/create_user ${GLOBAL} ./cmd/create_user.go
	go build ${LDFLAGS} -o ${PREFIX}/create_root ${GLOBAL} ./cmd/create_root.go
	go build ${LDFLAGS} -o ${PREFIX}/class_create ${GLOBAL} ./cmd/class_create.go
	go build ${LDFLAGS} -o ${PREFIX}/course_create ${GLOBAL} ./cmd/course_create.go
	go build ${LDFLAGS} -o ${PREFIX}/chapter_create ${GLOBAL} ./cmd/chapter_create.go
	go build ${LDFLAGS} -o ${PREFIX}/gen_token ${GLOBAL} ./cmd/gen_token.go
	go build ${LDFLAGS} -o ${PREFIX}/gen_questions ${GLOBAL} ./cmd/gen_questions.go
	@cp -v cmd/gen-cert ${PREFIX}

test:
	@echo "test"

clean:
	@rm -rvf dist

sync:
	go mod tidy
